<?php
/* 
 * Generate random upcoming investment candidates for a simluation.
 */

require_once('environment.php');
require_once('simulation.php');
require_once('data_access.php');

init_environment();

/* Generate a random number, within a range, of new candidate investments,
 * ensuring that the total number of candidate investments does not exceed
 * the simulation limit. */
$rand_num_records = rand($_CONFIG->simulation_gen_candidates_min,
                         $_CONFIG->simulation_gen_candidates_max);
$num_available = $_CONFIG->simulation_max_candidate - read_num_candidates();
$rand_num_records = min($rand_num_records, $num_available);
if (empty($rand_num_records)) {
    throw new Exception('Reached simulation candidate investment limit');
}

for ($i = 0; $i < $rand_num_records; $i++) {

    // write pseudo investment record
    $investment_id = write_investment(
        simulate_text($_CONFIG->form_max_title_len, 70, true),
        simulate_text($_CONFIG->form_max_desc_len, 50),
        simulate_charity_name(),
        SIMULATED_EXTERNAL_URL,
        simulate_username(),
        simulate_date(),
        simulate_investment_img());
    // function for reading investment adorns record with additional properties
    // required to write comments
    $investment = read_investment($investment_id);

    // attach a random number of comments
    $rand_num_comments = rand(0, $_CONFIG->simulation_gen_comments_max);
    for ($j = 0; $j < $rand_num_comments; $j++) {
        write_comment(
            $investment,
            simulate_username(),
            simulate_text($_CONFIG->form_max_comment_len, 85),
            simulate_date());
    }
}

?>
